## Choosing elements of a set at random (rd_choice)

from PyM import *

# def string(C):
#     s = ''
#     for c in C: s += c
#     return s
# 
# def rd_choice(X, m=1): 
#     if isinstance(X,int):
#         if X > 0: X = list(range(X))
#         else: return 'rd_choice error: {} is not positive'.format(X)
#     if isinstance(X,Vector_type): X = list(X)
#     if not (isinstance(X,list) or isinstance(X,str)):
#         return 'rd_choice error: wrong type of first parameter'
#     if len(X)==0: return None
#     if len(X)<m: 
#         return "rd_choice error: not enough items to choose from"
#     Y = clone(X)
#     Z = []
#     for j in range(m):
#         t = rd_int(0,len(Y)-1)
#         y = Y[t]
#         Z += [y]
#         Y = Y[:t] + Y[t+1:]
#     if isinstance(X,str): Z = string(Z)
#     return Z
# rd_selection = rd_sample = rd_choice

# Examples

show(rd_choice(-10))

show(rd_choice(10))

show(rd_choice('abcdefg'))

show(rd_choice(10,5))

X = [2] + [n for n in range(3,100,2) if is_prime(n)]
show(rd_choice(X,5))

X = vec(X,Z_)
show(rd_choice(X,5))

show(rd_choice('abcdefg',3))